########################################################################
# parms.py - Loads the system parameters file, which controls the
#	program's distribution-specific behaviors.  Sorta like preferences,
#	but usually they're only changed by whoever packages the programs
#	for distribution; certainly the program itself never changes the
#	parameters.
#
# Created 2006.08.30 by Warren Young
#
# Copyright (c) 2006 by Pixel Corps.  This program may be used under
# the terms of the X11 license, a copy of which should have accompanied
# this program, in the LICENSE file.
########################################################################

import ConfigParser, os, util


class Iterator:
	"""
	Simple iterator for moving through the parameter list.  Should only
	be used directly from within the module.
	"""

	def __init__(self, parms):
		self.list_ = parms.keys()
		self.list_.sort()
		self.index_ = -1

	def next(self):
		self.index_ += 1
		if self.index_ < len(self.list_):
			return self.list_[self.index_]
		else:
			raise StopIteration


class Parameters:
	"""
	Class to hold system-wide parameters that allow different groups to
	make the system behave in ways specific to that group's needs.
	"""
	
	# Default parameter values. Overridden on a per-instance basis where
	# values exist in the parameter file.
	parms_ = {
		'systemshortname': 'WickDD',
		'systemlongname': 'Wick Digital Dailies System',
		'uploaddirectorydefault': '',
		'uploaddirectoryvariable': 'Upload dir. variable',
	}

	def __init__(self, progsection):
		"""
		Loads the global and 'progsection' parts of the parameter
		file, using defaults where no value exists in the file.
		
		@type progsection: string
		@param progsection: name of INI file section to load in addition
		to the global parameters. Allows for program-specific
		parameters.
		"""

		pfn = util.FindAppResource('parameters.ini')
		if pfn:
			# Override defaults using values in parameter file
			cp = ConfigParser.SafeConfigParser()
			try:
				cp.read(pfn)
				print 'PARMS: Found parameters in', pfn
				try:
					for opt in cp.options('global'):
						self.parms_[opt.lower()] = cp.get(
								'global', opt, raw = True)
					for opt in cp.options(progsection):
						self.parms_[opt.lower()] = cp.get(
								progsection, opt, raw = True)
				except ConfigParser.NoSectionError:
					pass
			except:
				print 'PARMS: Failed to load %s; using defaults.' % pfn
		else:
			print "PARMS: Can't find parameter file; using defaults."


	def __getitem__(self, key):
		k = key.lower()
		if k in self.parms_:
			return self.parms_[k]
		else:
			return None


	def __iter__(self):
		return Iterator(self)


	def __len__(self):
		return len(self.parms_)


	def getVersion(self):
		"""
		Get the system version

		@rtype: string
		@return: the system version
		"""
	
		try:
			fVer = file(util.FindAppResource("version.txt"))
			return fVer.readline()
			fVer.close()
		except:
			return "UNKNOWN"


	def keys(self):
		"""
		Get the parameter names, those being keys into a hash of
		parameters.

		@rtype: array of string
		@return: parameter names
		"""
		return self.parms_.keys()

